home *** CD-ROM | disk | FTP | other *** search
/ The Very Best of Atari Inside / The Very Best of Atari Inside 1.iso / mint / mntlib43 / mntlib / isatty.c < prev    next >
C/C++ Source or Header  |  1993-09-22  |  2KB  |  82 lines

  1. /* from the original GCC TOS library by jrd */
  2. /* this algorithm is due to Allan Pratt @ Atari.  Thanks Allan! */
  3.  
  4. #include <errno.h>
  5. #include <osbind.h>
  6. #include <fcntl.h>
  7. #include <stdio.h>
  8. #include <ioctl.h>
  9. #include <unistd.h>
  10. #include <string.h>
  11. #include <support.h>
  12. #include <stat.h>
  13. #include <mintbind.h>
  14. #include "lib.h"
  15.  
  16. struct __open_file __open_stat[__NHANDLES];
  17.  
  18. int isatty(fd)
  19. int fd;
  20. {
  21.   int rc;
  22.   long oldloc;
  23.   int handle = __OPEN_INDEX(fd);
  24.  
  25.   if (handle < __NHANDLES)
  26.     if (__open_stat[handle].status != FH_UNKNOWN)
  27.         return(__open_stat[handle].status == FH_ISATTY);
  28.   oldloc = Fseek(0L, fd, SEEK_CUR);    /* save current location */
  29.   if (Fseek(1L, fd, SEEK_CUR) != 0) {    /* try to seek ahead one byte */
  30.     /* got either a file position or an error (usually EBADARG indicating
  31.        a range error from trying to seek past EOF), so it is not a tty */
  32.     rc = 0;
  33.     (void) Fseek(oldloc, fd, SEEK_SET);    /* seek back to original location */
  34.   }
  35.   else
  36.     rc = 1;                /* yes, tty */
  37.   if (handle < __NHANDLES)
  38.     if (rc) {
  39.         __open_stat[handle].status = FH_ISATTY;
  40.         __open_stat[handle].flags = CRMOD|ECHO;
  41.     }
  42.     else
  43.         __open_stat[handle].status = FH_ISAFILE;
  44.   return (rc);            /* return true, false, or error */
  45. }
  46.  
  47. /* _isctty():  determine if a file descriptor refers to this process's
  48.    controlling tty.
  49. */
  50. int
  51. _isctty(fd)
  52.   int fd;
  53. {
  54. #if 0
  55.   char ctty_name[L_ctermid];
  56.   char ftty_name[L_ctermid];
  57. #endif
  58.   struct stat st, tt;
  59.   extern int __mint;
  60.  
  61.   if (!(isatty(fd)) || !(isatty(-1)))
  62.     return 0;
  63.   if (fd == -1)
  64.     return 1;
  65. #if 1
  66.   if (__mint >= 9 && !Fcntl (fd, &st, FSTAT) && !Fcntl (-1, &tt, FSTAT)) {
  67.     /* shouldn't this be as good?  the stuff below takes ages...
  68.        (still loses on /dev/aux etc but ttyname can't be much better)
  69.     */
  70.     return (st.st_dev == tt.st_dev && st.st_ino == tt.st_ino);
  71.   }
  72.   /* We know that __mint < 9 (the Fcntl's above don't have the chance
  73.      to fail), use the same algorithm that ttyname() uses in this
  74.      case: it returns "/dev/aux" if fd == -2 */
  75.   return fd != -2;
  76. #else
  77.   (void) ctermid(ctty_name);
  78.   (void) strncpy(ftty_name, ttyname(fd), L_ctermid);
  79.   return !(strncmp(ctty_name, ftty_name, L_ctermid));
  80. #endif
  81. }
  82.